package com.link.play.fit.ai.ui.guide

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.link.play.fit.ai.bean.UserGuide
import com.link.play.fit.ai.databinding.FgGenderBinding
import com.link.play.fit.ai.ui.guide.ac.GuidePbAc
import com.link.play.fit.ai.ui.setting.SetUserInfoAc
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch


class GenderFg : BasePageFragment<FgGenderBinding>(FgGenderBinding::inflate) {

    var userGuideState: MutableStateFlow<UserGuide> = MutableStateFlow(UserGuide())

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        when (val ac = requireActivity()) {
            is GuidePbAc -> {// 引导页面
                userGuideState = ac.viewModel.userGuideState
            }

            is SetUserInfoAc -> {//设置界面的要用
                userGuideState = ac.viewModel.userGuideState
                binding.tvTitle.visibility = View.GONE
                binding.tvContent.visibility = View.GONE
            }

        }
        lifecycleScope.launch {
            userGuideState.collectLatest {
                when (it.gender) {
                    0 -> {
                        binding.tvMale.isSelected = true
                        binding.tvFemale.isSelected = false
                        binding.tvOther.isSelected = false
                    }

                    1 -> {
                        binding.tvFemale.isSelected = true
                        binding.tvMale.isSelected = false
                        binding.tvOther.isSelected = false
                    }

                    2 -> {
                        binding.tvOther.isSelected = true
                        binding.tvFemale.isSelected = false
                        binding.tvMale.isSelected = false
                    }

                    else -> {
                    }

                }
            }
        }

        binding.tvMale.setOnClickListener {
            userGuideState.update {
                it.copy(gender = 0)
            }
        }
        binding.tvFemale.setOnClickListener {
            userGuideState.update {
                it.copy(gender = 1)
            }
        }
        binding.tvOther.setOnClickListener {
            userGuideState.update {
                it.copy(gender = 2)
            }
        }
    }

    override fun onResume() {
        super.onResume()
    }
}